# -*- encoding:utf-8 -*-
import requests
from bs4 import BeautifulSoup


class pcScrawler():
    def __init__(self):
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95'
        }

    # 爬取文本
    def getTargetText(self, url, selector):
        codes = requests.get(url, headers=self.headers).text
        bs = BeautifulSoup(codes, 'html.parser')
        return [item.text for item in bs.select(selector=selector)]

    # 爬取图片
    def getTargetImg(self, imgurl):
        imgc = requests.get(imgurl).content
        with open("8002.jpg", 'wb') as f:
            f.write(imgc)


if __name__ == '__main__':
    url = "https://new.qq.com/rain/a/20230409A055M800"
    s = pcScrawler()
    imgurl = "https://inews.gtimg.com/om_bt/OPj4-LYYkUjCWd31zrKUBui24tUbX6Q64_9sUXEJgkDl4AA/1000"
    print(s.getTargetText(url, selector='p'))
    print(s.getTargetImg(imgurl))
